package com.bbs.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.search.ScoreDoc;

import com.bbs.dao.SectionDao;
import com.bbs.dao.TopicDao;
import com.bbs.dao.impl.ReplyDaoImpl;
import com.bbs.dao.impl.SectionDaoImpl;
import com.bbs.dao.impl.TopicDaoImpl;
import com.bbs.model.Reply;
import com.bbs.model.Section;
import com.bbs.model.Topic;
import com.bbs.permission.Permission;
import com.bbs.util.FTSutil;

public class TopicAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Topic topic;
	private TopicDao topicDao;
	private int id;
	private List<Topic> topics; 
	private List<Reply> replyList;
	private Map<Reply, List<Reply>> topicpmap;
	private Section section;
	ScoreDoc[] hits;

	//showAddTopic page
	public String showAddTopic(){
		return "showAddTopic";
	}
	//删除帖子
	public String delTopic() throws IOException{
		topicDao=new TopicDaoImpl();
		topicDao.delete(topic.getId());
		return "addTopic";
	}
	//增加帖子
	@Permission(resource = "Topic", action = "add")
	public String addTopic() throws IOException{
		topic.setPosttime
			(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		topicDao=new TopicDaoImpl();
		topicDao.save(topic);
		return "addTopic";
	}
	public String showUpdateTopic() throws IOException{
		
		topicDao=new TopicDaoImpl();
		topicDao.update(topic);
		
		return "showUpdateTopic";
	}
	
	//搜索帖子
	public String searchTopic() throws IOException{
		//查出所有帖子
		topicDao=new TopicDaoImpl();
		topics=topicDao.getTopicList();
		//在工具类中查找
		FTSutil fts=new FTSutil(topics);
		topics=fts.searchIndex(topic);
		//fts.closeReader();
		return "searchTopic";
	}
	//修改帖子
	public String updateTopic() throws IOException{
		
		topicDao=new TopicDaoImpl();
		topicDao.update(topic);
		
		return "updateTopic";
	}
	//显示帖子内容及其回复
	//@Permission(resource = "Topic", action = "read")
	public String readTopic() throws IOException{
		//帖子的访问量加1
		new TopicDaoImpl().addClic_count(id);
		
		topicDao=new TopicDaoImpl();
		//id=Integer.parseInt(getRequest().getParameter("id"));
		//HttpServletRequest request = ServletActionContext.getRequest();
		topic=topicDao.findById(id);
		//查看帖子的同时展现所有对帖子的评论
		replyList=new ReplyDaoImpl().getReplyList(id);
		topicpmap=new HashMap<Reply, List<Reply>>();
		for(Reply reply:replyList){//遍历所有回复记录
			if(reply.getReply_parent_id()==0){//对主贴的直接回复
				topicpmap.put(reply, new ArrayList<Reply>());			
			}else {//对回复的回复
				Set<Reply> keys=topicpmap.keySet();//对主贴回复的集合
				for(Reply  r:keys){
					//同一回复下面的回复放在一个list中
					if(reply.getReply_parent_id()==r.getReply_id()){
						topicpmap.get(r).add(reply);
					}
				}
			}
		}
		return "readTopic";
	}
	/*public String readTopicLayered() throws IOException{
		topicDao=new TopicDaoImpl();
		id=Integer.parseInt(getRequest().getParameter("id"));
		topic=topicDao.findById(id);
		replyList=new ReplyDaoImpl().getReplyList(id);
		topicpmap=new HashMap<Reply, List<Reply>>();
		for(Reply reply:replyList){
			if(reply.getReply_parent_id()==0){
				topicpmap.put(reply, new ArrayList<Reply>());			
			}else {
				Set<Reply> keys=topicpmap.keySet();
				for(Reply  r:keys){
					if(reply.getReply_parent_id()==r.getReply_id()){
						topicpmap.get(r).add(reply);
					}
				}
			}
		}
		return "readTopicLayered";
	}*/
	/**
	 * 得到所有帖子
	 * @return
	 * @throws IOException
	 */
	public String  getTopicList() throws IOException{		
		topicDao=new TopicDaoImpl();
		topics=topicDao.getTopicList();
		return "topicList";
	}
	/**
	 * 得到对应版块下 的所有帖子,点击板块，增加了板块的点击量
	 * @return
	 * @throws IOException
	 */
	public String  getTopicListBySectionId() throws IOException{		
		topicDao=new TopicDaoImpl();
		//int section_id=Integer.parseInt(getRequest().getParameter("section_id"));
		topics=topicDao.getTopicListBySectionId(section.getSection_id());
		
		SectionDao  sectionDao=new SectionDaoImpl();
		//业务逻辑在action层实现，不要在Dao层实现 .增加点击量
		new SectionDaoImpl().addClick_count(section.getSection_id());
		section=sectionDao.findById(section.getSection_id());
		
		return "topicListBySectionId";
	}
	
	//全文检索系统
	public String fullTextSearch(){
		
		return "fullTextSearch";
	}
	//版主管理的板块的帖子
	public String topicsOfModerator(){
		return "topicsOfModerator";
	}
	public Topic getTopic() {
		return topic;
	}
	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	public List<Topic> getTopics() {
		return topics;
	}
	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}
	public List<Reply> getReplyList() {
		return replyList;
	}
	public void setReplyList(List<Reply> replyList) {
		this.replyList = replyList;
	}
	public Map<Reply, List<Reply>> getTopicpmap() {
		return topicpmap;
	}
	public void setTopicpmap(Map<Reply, List<Reply>> topicpmap) {
		this.topicpmap = topicpmap;
	}
	public Section getSection() {
		return section;
	}
	public void setSection(Section section) {
		this.section = section;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public ScoreDoc[] getHits() {
		return hits;
	}
	public void setHits(ScoreDoc[] hits) {
		this.hits = hits;
	}
	
}
